//
//  AccountViewController.m
//  MoneyLog
//
//  Created by Max on 11-6-18.
//  Copyright 2011年 wicresoft. All rights reserved.
//

#import "AccountViewController.h"
#import "AccountWrapper.h"
#import "AccountTypeListViewController.h"


extern NSString *kRowTextKey;
extern NSString *kRowItemKey;
extern NSString *kRowAccessTypeKey;


@implementation AccountViewController
@synthesize titleString;
@synthesize _tableView;
@synthesize account;
@synthesize dataSourceArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [titleString release];
    [_tableView release];
    [account release];
    [dataSourceArray release];
    
    [accountNameTextField release];
    [accountTypeLabel release];
    [moneyKindLabel release];
    [moneyStartTextField release];
    [moneyTransferInLabel release];
    [moneyTransferOutLabel release];
    [moneyIncomeLabel release];
    [moneyExpenseLabel release];
    [moneyLabel release];
    [moneyAlarmSwitch release];
    [moneyAlarmTextField release];
    
    [accountTypeViewController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = titleString;
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] 
                                initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                target:self action:@selector(saveAction)];
    self.navigationItem.rightBarButtonItem = saveItem;
    [saveItem release];
    
    accountNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100,10,180, 27)];
    accountNameTextField.borderStyle = UITextBorderStyleNone;
    accountNameTextField.textColor = [UIColor blackColor];
    accountNameTextField.textAlignment = UITextAlignmentRight;
    accountNameTextField.font = [UIFont systemFontOfSize:17.0]; 
    accountNameTextField.backgroundColor = [UIColor whiteColor];
    accountNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;	 
    accountNameTextField.keyboardType = UIKeyboardTypeDefault;	 
    accountNameTextField.returnKeyType = UIReturnKeyDefault;
    accountNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;	
    accountNameTextField.text = account.name;
    accountNameTextField.delegate = self;	
    
    moneyStartTextField = [[UITextField alloc] initWithFrame:CGRectMake(100,10,180, 27)];
    moneyStartTextField.borderStyle = UITextBorderStyleNone;
    moneyStartTextField.textColor = [UIColor blackColor];
    moneyStartTextField.textAlignment = UITextAlignmentRight;
    moneyStartTextField.font = [UIFont systemFontOfSize:17.0]; 
    moneyStartTextField.backgroundColor = [UIColor whiteColor];
    moneyStartTextField.autocorrectionType = UITextAutocorrectionTypeNo;	 
    moneyStartTextField.keyboardType = UIKeyboardTypeDefault;	 
    moneyStartTextField.returnKeyType = UIReturnKeyDefault;
    moneyStartTextField.clearButtonMode = UITextFieldViewModeWhileEditing;	
    moneyStartTextField.text = [NSString stringWithFormat:@"%.2f",account.moneyStart];
    moneyStartTextField.delegate = self;
    
    moneyAlarmTextField = [[UITextField alloc] initWithFrame:CGRectMake(100,10,180, 27)];
    moneyAlarmTextField.borderStyle = UITextBorderStyleNone;
    moneyAlarmTextField.textColor = [UIColor blackColor];
    moneyAlarmTextField.textAlignment = UITextAlignmentRight;
    moneyAlarmTextField.font = [UIFont systemFontOfSize:17.0]; 
    moneyAlarmTextField.backgroundColor = [UIColor whiteColor];
    moneyAlarmTextField.autocorrectionType = UITextAutocorrectionTypeNo;	 
    moneyAlarmTextField.keyboardType = UIKeyboardTypeDefault;	 
    moneyAlarmTextField.returnKeyType = UIReturnKeyDefault;
    moneyAlarmTextField.clearButtonMode = UITextFieldViewModeWhileEditing;	
    moneyAlarmTextField.text = [NSString stringWithFormat:@"%.2f",account.moneyStart];
    moneyAlarmTextField.delegate = self;

    accountTypeLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [accountTypeLabel setFont:[UIFont systemFontOfSize:16.0]];
    accountTypeLabel.textAlignment = UITextAlignmentRight;
    
    moneyKindLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [moneyKindLabel setFont:[UIFont systemFontOfSize:16.0]];
    moneyKindLabel.textAlignment = UITextAlignmentRight;

    
    moneyTransferInLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [moneyTransferInLabel setFont:[UIFont systemFontOfSize:16.0]];
    moneyTransferInLabel.textAlignment = UITextAlignmentRight;
    
    moneyTransferOutLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [moneyTransferOutLabel setFont:[UIFont systemFontOfSize:16.0]];
    moneyTransferOutLabel.textAlignment = UITextAlignmentRight;
    
    moneyIncomeLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [moneyIncomeLabel setFont:[UIFont systemFontOfSize:16.0]];
    moneyIncomeLabel.textAlignment =UITextAlignmentRight;
    
    moneyExpenseLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [moneyExpenseLabel setFont:[UIFont systemFontOfSize:16.0]];
    moneyExpenseLabel.textAlignment = UITextAlignmentRight;
    
    moneyLabel=[[UILabel alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    [moneyLabel setFont:[UIFont systemFontOfSize:16.0]];
    moneyLabel.textAlignment = UITextAlignmentRight;

    moneyAlarmSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(200, 8, 80, 27)];
    moneyAlarmSwitch.on = account.alarmValid;
    
    accountTypeViewController = [[AccountTypeListViewController alloc] initWithStyle:UITableViewStylePlain];
    accountTypeViewController.typeName = account.type;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
   
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setToolbarHidden:NO animated:YES];
    
    
    accountNameTextField.text =account.name;
    accountTypeLabel.text = accountTypeViewController.typeName;
    moneyKindLabel.text = account.moneyKind;
    moneyStartTextField.text = [NSString stringWithFormat:@"%.2f",account.moneyStart];
    moneyTransferInLabel.text =[NSString stringWithFormat:@"%.2f",account.moneyTransferIn];
    moneyTransferOutLabel.text =[NSString stringWithFormat:@"%.2f",account.moneyTransferOut];
    moneyIncomeLabel.text = [NSString stringWithFormat:@"%.2f",account.moneyIncome];
    moneyExpenseLabel.text =[NSString stringWithFormat:@"%.2f",account.moneyExpense];
    moneyLabel.text = [NSString stringWithFormat:@"%.2f",account.money];
    moneyAlarmSwitch.on = account.alarmValid;
    moneyAlarmTextField.text =[NSString stringWithFormat:@"%.2f",account.moneyAlarm];
    
    self.dataSourceArray = [[NSMutableArray alloc] initWithObjects:
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"帐户名称",kRowTextKey,
                             accountNameTextField,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"帐户类型",kRowTextKey,
                             accountTypeLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryDisclosureIndicator],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"币种",kRowTextKey,
                             moneyKindLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"起始金额",kRowTextKey,
                             moneyStartTextField,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"转入金额",kRowTextKey,
                             moneyTransferInLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"转出金额",kRowTextKey,
                             moneyTransferOutLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"支出金额",kRowTextKey,
                             moneyExpenseLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"收入金额",kRowTextKey,
                             moneyExpenseLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"可用余额",kRowTextKey,
                             moneyLabel,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"启用报警",kRowTextKey,
                             moneyAlarmSwitch,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"报警金额",kRowTextKey,
                             moneyAlarmTextField,kRowItemKey,
                             [NSNumber numberWithInt:UITableViewCellAccessoryNone],kRowAccessTypeKey,nil ],
                            nil];
    [self._tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setToolbarHidden:YES animated:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - * Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //return [[self.dataSourceArray objectAtIndex:section] objectForKey:kSectionTitleKey];
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString *title = nil;
	
	return title;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (account.alarmValid) {
        return [dataSourceArray count];
    }
    else
    {
        return [dataSourceArray count]-1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [[dataSourceArray objectAtIndex:indexPath.row] objectForKey:kRowTextKey];
    [cell.contentView addSubview:[[dataSourceArray objectAtIndex:indexPath.row] objectForKey:kRowItemKey]];
    cell.accessoryType = [[[self.dataSourceArray objectAtIndex:indexPath.row] objectForKey:kRowAccessTypeKey] intValue];
    
    return cell;
}

//// Override to support conditional editing of the table view.
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // Return NO if you do not want the specified item to be editable.
//    return NO;
//}
//
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
//
//// Override to support editing the table view.
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        // Delete the row from the data source
//    }   
//    else if (editingStyle == UITableViewCellEditingStyleInsert) {
//        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
//    }   
//    
//}
//
//- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
//{
//	NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);
//}
//
//- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
//{
//	NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);
//}

//
//// Override to support rearranging the table view.
//- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
//{
//}
//
//// Override to support conditional rearranging of the table view.
//- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // Return NO if you do not want the item to be re-orderable.
//    return YES;
//}


#pragma mark - * Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row ==1) {
        [self.navigationController pushViewController:accountTypeViewController animated:YES];
    }
}

#pragma mark - * UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	// the user pressed the "Done" button, so dismiss the keyboard
	[textField resignFirstResponder]; 
    //record.money=[textField.text floatValue];
	return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    if ([textField.text length] != 0) {
        //record.money = [textField.text floatValue];
    }
    
    return YES;
}


#pragma mark - * Actions

-(void)cancelAction{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

-(void)saveAction 
{
    
}

-(void)initData
{

}

@end
